class User < ActiveRecord::Base
  has_many :lecture_ratings
  has_many :exercise_ratings

  validates :nickname,  :presence => true
  validates :pwd,	    :presence => true,
                        :length => { :minimum => 5 }

  attr_accessible :email, :pwd, :nickname

  attr_accessor :pwd
  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i

  def self.authenticate(username_or_email="", login_password="")
    if  EMAIL_REGEX.match(username_or_email)
      user = User.find_by_email(username_or_email)
    else
      user = User.find_by_nickname(username_or_email)
    end

    if user #&& user.pwd == login_password
      return user
    else
      return false
    end
  end
end
